<template>
  <basic-container>
    <avue-crud
        ref="curd"
        v-model="form"
        :before-open="beforeOpen"
        :data="data"
        :option="option"
        :page="page"
        :permission="permissionList"
        :table-loading="loading"
        @row-save="save"
        @row-del="del"
        @row-update="update"
        @current-change="currentChange"
        @size-change="sizeChange"
        @search-change="search"
        @refresh-change="refresh"
        @selection-change="selectChange"
        @on-load="onLoad"
    >
      <template slot="requestTime" slot-scope="scope">
        <el-tag type="info">{{ scope.row.requestTime }} ms</el-tag>
      </template>
      <template slot="requestMethod" slot-scope="scope">
        <el-tag>{{ scope.row.requestMethod }}</el-tag>
      </template>
      <template slot="logType" slot-scope="scope">
        <el-tag v-if="scope.row.logType == 1" type="success">{{
            scope.row.$logType
          }}
        </el-tag>
        <el-tag v-if="scope.row.logType == 2" type="danger">{{
            scope.row.$logType
          }}
        </el-tag>
      </template>
    </avue-crud>
  </basic-container>
<!--  <div class="app-container">-->

<!--  </div>-->
</template>
<script>
import {mapGetters} from "vuex";
import {del, exports, pageList, query, save, update} from "@/api/monitor/log";

export default {
  data() {
    return {
      loading: true,
      form: {},
      data: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        menu:false,
        menuWidth: 300,
        height: 'auto',
        calcHeight: 100,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        selection: true,
        dialogClickModal: false,
        index: true,
        enter: true,
        copyBtn: true,
        viewBtn: true,
        columnBtn:false,
        tip:false,
        column: [
          {
            label: "操作账号",
            prop: "username",
            rules: [
              {
                required: true,
                message: "请输入操作账号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "访问url",
            prop: "url",
            rules: [
              {
                required: true,
                message: "请输入访问url",
                trigger: "blur",
              },
            ],
          },
          {
            label: "来访Ip",
            prop: "ip",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入来访Ip",
                trigger: "blur",
              },
            ],
          },
          {
            label: "浏览器",
            prop: "browser",
            rules: [
              {
                required: true,
                message: "请输入浏览器",
                trigger: "blur",
              },
            ],
          },
          {
            label: "描述",
            prop: "remark",
          },
          {
            label: "执行时间",
            prop: "requestTime",
            slot: true,
          },
          {
            label: "请求方法",
            prop: "method",
            rules: [
              {
                required: true,
                message: "请输入请求方法",
                trigger: "blur",
              },
            ],
          },
          {
            label: "请求方式",
            prop: "requestMethod",
            search: true,
            slot: true,
            rules: [
              {
                required: true,
                message: "请输入请求方式",
                trigger: "blur",
              },
            ],
          },
          {
            label: "请求参数",
            prop: "parameters",
            hide: true,
          },
          {
            label: "日志类型",
            type: "select",
            slot: true,
            prop: "logType",
            search: true,
            props: {
              label: "name",
              value: "dictValue",
            },
            dicUrl: `/system/dict/netDict/log`,
            rules: [
              {
                required: true,
                message: "请选择日志类型",
                trigger: "click",
              },
            ],
          },
          {
            label: "部门",
            prop: "deptId",
            search: true,
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/system/dept`,
            rules: [
              {
                required: false,
                message: "请输入部门id",
                trigger: "blur",
              },
            ],
          },
        ],
      },
    };
  },
  computed: {
    /* eslint-disable */
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        copyBtn: this.vaildData(this.permission.log_copy, false),
        viewBtn: this.vaildData(this.permission.log_view, false),
        addBtn: this.vaildData(this.permission.log_add, false),
        delBtn: this.vaildData(this.permission.log_delete, false),
        editBtn: this.vaildData(this.permission.log_edit, false),
      };
    },
    delIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
  },
  methods: {
    onLoad(page, param) {
      this.loading = true;
      pageList(page.currentPage, page.pageSize, param).then((res) => {
        this.loading = false;
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
        console.log(this.page.total)
      });
    },
    exports() {
      let url = exports();
      const a = document.createElement("a");
      a.href = url;
      a.click();
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        query(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    save(row, loading, done) {
      loading();
      save(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    deletaAll() {
      if (this.selectionList.length <= 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(this.delIds);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    update(row, index, loading, done) {
      loading();
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    currentChange() {
    },
    refresh() {
      this.onLoad(this.page, "");
    },
    sizeChange() {
    },
    search(params, done) {
      this.onLoad(this.page, params);
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
